package edu.ahpu.propertymanage.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import edu.ahpu.propertymanage.dataclass.CostModel;
import edu.ahpu.propertymanage.pojo.ManageCar;
import edu.ahpu.propertymanage.pojo.PropertyTage;
import edu.ahpu.propertymanage.service.ManageCarService;
import edu.ahpu.propertymanage.service.PropertyTageService;
import edu.ahpu.propertymanage.util.TimeChange;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/managecar")
public class ManageCarCostController {

    @Autowired
    ManageCarService manageCarService;

    @Autowired
    PropertyTageService propertyTageService;

    @Autowired
    TimeChange timeChange;

    @PostMapping("managecar")
    public ManageCar showManageCar(String ownerId) {
        QueryWrapper<ManageCar> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(ManageCar::getOwnerId, ownerId);
        ManageCar manageCar = manageCarService.getOne(wrapper);

        return manageCar;
    }

    @PostMapping("addcar")
    public String addCar(@RequestBody ManageCar manageCar) {
        ManageCar manageCar1 = manageCarService.getById(manageCar.getManageCarId());
        if (!manageCar.equals(manageCar1)) {
            boolean bool = manageCarService.updateById(manageCar);
            if (bool)
                return "success";
            else
                return "error";
        } else
            return "error";
    }

    @PostMapping("parkingcost")
    public CostModel parkingCost(String ownerId) {
        QueryWrapper<ManageCar> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(ManageCar::getOwnerId, ownerId);
        ManageCar manageCar = manageCarService.getOne(wrapper);
        manageCar.setParkingBeginTime(timeChange.deleteTime(manageCar.getParkingBeginTime()));

        PropertyTage propertyTage = propertyTageService.getById(manageCar.getParkingPrice());

        List<Object> list = new ArrayList<>();
        list.add(manageCar);
        list.add(propertyTage);

        CostModel costModel = new CostModel();
        costModel.setList(list);

        return costModel;
    }

    @PostMapping("parkingprice")
    public CostModel parkingPrice(@RequestBody ManageCar manageCar) {
        PropertyTage propertyTage = propertyTageService.getById(manageCar.getParkingPrice());
        manageCar.setParkingTotalCost(manageCar.getParkingTime() * propertyTage.getPrice());
        manageCar.setParkingBeginTime(timeChange.deleteTime(manageCar.getParkingBeginTime()));
        manageCar.setParkingEndTime(timeChange.deleteTime(timeChange.addSetMonthTime(manageCar.getParkingBeginTime(), manageCar.getParkingTime())));


        List<Object> list = new ArrayList<>();
        list.add(manageCar);
        list.add(propertyTage);

        CostModel costModel = new CostModel();
        costModel.setList(list);

        return costModel;
    }


}
